package Exercise;

/**
 *  求单链表中有效节点的个数
 */
public class Exercise1 {
    public static void main(String[] args) {
        SinleLinkedList sinleLinkedList = new SinleLinkedList();
        sinleLinkedList.add(new Node(3,"刘希南"));
        sinleLinkedList.add(new Node(2,"郑清波"));
        sinleLinkedList.add(new Node(1,"陈俊华"));
        sinleLinkedList.show();
        sinleLinkedList.deleteById(2);
        sinleLinkedList.show();
        int lenght = sinleLinkedList.lenght();
        System.out.println(lenght);
        Node index = sinleLinkedList.getIndex(sinleLinkedList.getHead(), 1);
        System.out.println(index);
    }
}
class Node{
    public Integer id;
    public String name;
    public Node next;

    public Node(Integer no, String name) {
        this.id = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
class SinleLinkedList{
    private  Node head = new Node(null,null);

    public Node getHead() {
        return head;
    }

    public  void add(Node node){
        Node temp = head;
        boolean isFlag = false;
        while (true){
            if (temp.next==null){
                break;
            } else if(temp.next.id>node.id){
                break;
            } else if(temp.next.id==node.id){
                isFlag=true;
                break;
            }
            temp=temp.next;
        }
        if (isFlag){
            System.out.println("该学生已经存在编号为:"+node.id+"名字为:"+node.name);
        }else {
            node.next=temp.next;
            temp.next=node;
        }
    }
  public void show(){
        Node temp = head;
        if(temp.next==null){
            System.out.println("该表为空");
        }
        while (temp.next!=null){
          temp=temp.next;
            System.out.println("该学生已经存在编号为:"+temp.id+"名字为:"+temp.name);
        }
      System.out.println("输出完毕");
  }
  public void deleteById(int id){
        Node temp = head;
        if (temp.next==null){
            System.out.println("链表为空删除不了");
        }
        while (true){
            if (temp.next.id==id){
                temp.next=temp.next.next;
                return;
            }
            temp=temp.next;
            if (temp==null){
                System.out.println("找不到该数据,删除失败");
                return;
            }
        }
  }
  public int lenght(){
      int count=0;
      Node temp = head;
      if (temp.next==null){
          return 0;
      }
      while (true){
          if (temp.next!=null){
              count++;
          }
          temp=temp.next;
          if (temp==null){
              return count;
          }
      }
  }
  //得到倒数第K个元素
  public Node getIndex(Node head,int index){
        Node temp = head;
        if (temp==null){
            throw new RuntimeException("链表为空,查找不到");
        }
        int lenght = lenght();
      if(index<=0 || index>lenght){
        throw  new RuntimeException("倒数第"+index+"不存在");
      }
      int count=0;
      while (count<(lenght-index)){
          temp=temp.next;
          count++;
      }
      return temp.next;
  }
}

